﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ProjectRoughWork
{
    class LevelTile
    {
        /// <summary>
        /// Different Tile types enum. Currently unused. NOTE: MAY BE REDUNDANT DUE TO LEVEL CLASS KEEPING TRACK OF THIS
        /// </summary>

        public enum TitleType { ground, water, cover };

        /// <summary>
        /// Defeault Constructor. Currently doesn't do anything
        /// </summary>

        public LevelTile()
        {
        }


        /// <summary>
        /// 3 Parameter Constructor that initializes the Level Tile
        /// </summary>
        /// <param name="initPosition">The initial position of the tile</param>
        /// <param name="initSize">The initial size of the tile</param>
        /// <param name="initTexture">The initial texture of the tile</param>

        public LevelTile(Vector3 initPosition, float initSize, Texture2D initTexture)
        {
            position = initPosition;
            size = initSize;
            texture = initTexture;

            Init();
        }

        /// <summary>
        /// Sets up the BasicEffect, vertices and indices that'll be used to draw the tile
        /// </summary>


        public void Init()
        {
            effect = Game1.GetBasicEffect();

            vertices = new VertexPositionNormalTexture[4];
            Vector3 temp = new Vector3(position.X, position.Y, position.Z);
            Vector3 normal = new Vector3(0.0f, 1.0f, 0.0f);

            vertices[0].Position = temp;
            vertices[0].Normal = normal;
            vertices[0].TextureCoordinate = new Vector2(0, 0);

            temp.X += size;

            vertices[1].Position = temp;
            vertices[1].Normal = normal;
            vertices[1].TextureCoordinate = new Vector2(1, 0);

            temp.Z += size;

            vertices[2].Position = temp;
            vertices[2].Normal = normal;
            vertices[2].TextureCoordinate = new Vector2(0, 1);

            temp.X -= size;

            vertices[3].Position = temp;
            vertices[3].Normal = normal;
            vertices[3].TextureCoordinate = new Vector2(1, 1);

            short[] vertIndices = { 0, 1, 2, 2, 3, 0 };

            indices = vertIndices;
            
        }

        /// <summary>
        /// Accessor and Mutator for the View matrix
        /// </summary>

        public Matrix View
        {
            get { return view; }
            set { view = value; }
        }

        /// <summary>
        /// Accessor and Mutator for the Projection matrix
        /// </summary>

        public Matrix Projection
        {
            get { return projection; }
            set { projection = value; }
        }

        /// <summary>
        /// Draws the level tile
        /// </summary>

        public void Draw()
        {
            effect.EnableDefaultLighting();
            effect.LightingEnabled = true;

            effect.World = Matrix.Identity;

            effect.View = view;
            effect.Projection = projection;

            effect.TextureEnabled = true;
            effect.Texture = texture;

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();

                Game1.GetGraphicsDevice().DrawUserIndexedPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, vertices, 0, 4, indices, 0, 2);
            }
        }

        //Data Members

        Matrix view;
        Matrix projection;
        BasicEffect effect;

        Vector3 position;
        float size;
        Texture2D texture;
        VertexPositionNormalTexture[] vertices;
        short[] indices;
        
    }
}
